package servlet.content;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import operate.OperateType;

import offer.GetDataSource;
import offer.GetNodeFromModule;

import com.netscenery.yang.model.*;

public class CaseOpreateServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public CaseOpreateServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		String       name         = request.getParameter("_name");
		String       status       = request.getParameter("_status");//post �ύʱ ������ status��Ϊ����
		String       featureParam = request.getParameter("featureParam"); //Feature Parameter
		String       description  = request.getParameter("description");
		String       nativeDescr  = request.getParameter("nativeDescr");
		String       reference    = request.getParameter("reference");
		String       nodeLevel    = request.getParameter("nodeId");   //ͨ������������ҽ��
		//String[]     levels       =  nodeLevel.split("_");
		String[]     levels       =  nodeLevel.split("\\\\");
		//������Դ
		//MainModule   module       = GetDataSource.getDataRead(request);
		Module   module       = GetDataSource.getDataRead(request);
		Case _case                =(Case)GetNodeFromModule.getDataDefByLervels(module, levels);

		String url ="/items/Case.jsp";
		
		if(!_case.getName().equalsIgnoreCase(name)){
			//�ڵ������
			if(_case.checkName(name)){
				_case.setName(name);
				request.setAttribute("opType", OperateType.MODIFY_INCLUDE_NAME);
			}else{
				url = "/Notice.jsp";
				request.setAttribute("msg", "�ڵ�����֤��ͨ��");
				ServletContext sc=this.getServletContext();
				RequestDispatcher rd=sc.getRequestDispatcher(url);
				rd.forward(request, response);
				return;
			}
		}else{
			request.setAttribute("opType", OperateType.MODIFY);
		}
		
		_case.setStatus(Status.getStatus(status));
		_case.setDescription(description.trim().equalsIgnoreCase("")?null:description.trim());
		_case.setReference(reference.trim().equalsIgnoreCase("")?null:reference.trim());
		
		/** *********************************************************************************
		 *  Featuer ����  Begin 
		 */
		int featuerLength = _case.getIf_featuresNumber();
		for(int k = 0; k < featuerLength ; k++)
			_case.removeIf_feature(0);
		if(!"".equalsIgnoreCase(featureParam.trim())){
			String[] featureArr = featureParam.split(",");
			for(int i = 0 ; i < featureArr.length ; i++){
				_case.addIf_feature(featureArr[i].trim());
			}
		}
		/** 
		 *  Featuer ����  End 
		 *  *********************************************************************************
		 */
		
		
		//��ݳ־û�
		GetDataSource.getDataWrite(request, module);
		request.setAttribute("itemNode", _case);
		request.setAttribute("nodeId",nodeLevel);
		
		ServletContext sc=this.getServletContext();
		RequestDispatcher rd=sc.getRequestDispatcher(url);
		rd.forward(request, response);
		
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
